// pages/choiceCoupon/choiceCoupon.js
const couponService = require('../../../../api/couponService.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    couponList: [],
    unableCouponList: [],
    allCouponList: [],
    freeshipCoupon: [],
    unableFreeshipCoupon: [],
    allDiscount: [],
    allFreeship: [],
    loading: true,
    couponType: 'discount'
  },
  couponClick: function (val) {
    var that = this
    var id = val.currentTarget.dataset.id;
    console.log(id)
    //设置当前样式
    if (val.currentTarget.dataset.type === 'freeship') {
      if (that.data.current === id) {
        that.setData({
          'current': ''
        })
      } else {
        that.setData({
          'current': id
        })
      }
    } else if (val.currentTarget.dataset.type === 'discount') {
      if (that.data.currentItem === id) {
        that.setData({
          'currentItem': ''
        })
      } else {
        that.setData({
          'currentItem': id
        })
      }
    }
  },
  toPerfectOrder: function () {
    const that = this
    let couponId = this.data.currentItem
    let freeshipId = this.data.current
    let chooseCouponData = [];
    let typeData = JSON.stringify(this.options.type);
    console.log(this.data.couponList)
    this.data.couponList.forEach(ele => {
      if (ele.coupon.id === couponId) {
        chooseCouponData.push(ele);
      }
    });
    this.data.freeshipCoupon.forEach(ele => {
      if (ele.coupon.id === freeshipId) {
        chooseCouponData.push(ele);
      }
    });
    console.log(chooseCouponData)
    if (chooseCouponData) {
      try {
        let tempData = {
          coupons: chooseCouponData,
          couponType: that.data.couponType
        }
        wx.setStorageSync('wj_chooseCoupon', tempData)
      } catch (e) { }
      wx.navigateBack({
        delta: 1
      })
    } else {
      wx.navigateBack({
        delta: 1
      })
    }
  },
  getAvailableCoupon: function (data) {
    const that = this
    let postData = {}
    const orderType = that.options.orderType
    if (orderType === 'normal') {
      postData = {
        order: data
      }
    } else if (orderType === 'secondkill') {
      postData = {
        grabOrder: data
      }
    } else if (orderType === 'advanceSell') {
      postData = {
        order: data
      }
    } else {
      postData = {
        order: data
      }
    }
    console.log(postData)
    couponService.getAvailableCoupon(postData).then(res => {
      console.log(res)
      wx.hideLoading()
      let tempAllCouponList = [];
      let tempCouponList = [];
      let tempUnableCouponList = [];
      let tempFreeshipCoupon = [];
      let tempUnableFreeshipCoupon = [];
      let tempAllFreeship = [];
      let tempAllDiscount = [];
      res.forEach(ele => {
        ele.coupon = {
          ...ele.coupon,
          rate: ele.coupon.rate || ele.coupon.rate === 0 ? (ele.coupon.rate * 10).toFixed(1) : '',
        }
        if (ele.coupon.bytimeStart) {
          let startTime = ele.coupon.bytimeStart.split(" ")[0] + ' ' + ele.coupon.bytimeStart.split(" ")[1].slice(0, 5)
          let endTime = ele.coupon.bytimeEnd.split(" ")[0] + ' ' + ele.coupon.bytimeEnd.split(" ")[1].slice(0, 5)
          // let effectiveTime = startTime + ' 至 ' + endTime
          // let startTime = ele.bytimeStart.slice(0, 16)
          // let endTime = ele.bytimeEnd.slice(0, 16)
          let effectiveTime = startTime + ' ~ ' + endTime
          ele.coupon = {
            ...ele.coupon,
            effectiveTime: effectiveTime
          }
        } else {
          ele.coupon = {
            ...ele.coupon,
            effectiveTime: ''
          }
        }
        if (ele.coupon.function === "FREESHIP") {
          ele.coupon = {
            ...ele.coupon,
            name: '运费券'
          }
          if (ele.usable) {
            tempFreeshipCoupon.push(ele)
          } else {
            tempUnableFreeshipCoupon.push(ele)
          }
          tempAllFreeship.push(ele)
        } else {
          if (ele.usable) {
            tempCouponList.push(ele)
          } else {
            tempUnableCouponList.push(ele)
          }
          tempAllDiscount.push(ele)
        }
        tempAllCouponList.push(ele)
      });
      this.setData({
        allCouponList: tempAllCouponList,
        couponList: tempCouponList,
        unableCouponList: tempUnableCouponList,
        freeshipCoupon: tempFreeshipCoupon,
        unableFreeshipCoupon: tempUnableFreeshipCoupon,
        allDiscount: tempAllDiscount,
        allFreeship: tempAllFreeship,
        loading: false,
      })
    }).catch(err => {
      wx.showToast({
        title: err.message,
        icon: 'none',
        duration: 2000
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu()
    console.log(this.options)
    let postData = JSON.parse(this.options.orderData)
    let couponType = this.options.couponType
    this.setData({
      couponType: couponType
    })
    this.getAvailableCoupon(postData)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})